package jiguang.ui.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.JGApplication;
import com.jiuyin.dianjing.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import jiguang.entity.Event;
import jiguang.entity.EventType;
import jiguang.ui.activity.base.BaseActivity;
import jiguang.ui.fragment.HomePageFragment;
import jiguang.util.DialogCreator;


/**
 * Created by ${chenyn} on 2017/5/10.
 */

public class GroupNotFriendActivity extends BaseActivity implements View.OnClickListener {

    private Button mBtn_add_friend;
    private Button mBtn_send_message;
    private ImageButton mBtn_return;
    private String mUserName;
    private UserInfo mUserInfo;
    private String mMyName;
    private String mNickName;
    private String mAvatarPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_not_friend);

        initView();
        initData();
    }

    private void initData() {
        final Dialog dialog = DialogCreator.createLoadingDialog(this, this.getString(R.string.jmui_loading));
        dialog.show();
        mUserName = getIntent().getStringExtra(JGApplication.TARGET_ID);
        HomePageFragment homePageFragment = HomePageFragment.newInstance(mUserName);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.layout_fragment, homePageFragment)
                .commit();
        JMessageClient.getUserInfo(mUserName, new GetUserInfoCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage, UserInfo info) {
                if (responseCode == 0) {
                    mUserInfo = info;
                }
                dialog.dismiss();
            }
        });

        UserInfo myInfo = JMessageClient.getMyInfo();
        mMyName = myInfo.getNickname();
        if (TextUtils.isEmpty(mMyName)) {
            mMyName = myInfo.getUserName();
        }
    }

    private void initView() {
        mBtn_send_message = (Button) findViewById(R.id.btn_send_message);
        mBtn_add_friend = (Button) findViewById(R.id.btn_add_friend);
        mBtn_return = (ImageButton) findViewById(R.id.return_btn);
        mBtn_return.setOnClickListener(this);
        mBtn_add_friend.setOnClickListener(this);
        mBtn_send_message.setOnClickListener(this);
    }

    public String getBirthday(UserInfo info) {
        long birthday = info.getBirthday();
        Date date = new Date(birthday);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return dateFormat.format(date);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btn_add_friend:
                if (mUserInfo.isFriend()) {
                    ToastUtil.showShort("对方已经是你的好友");
                } else {
                    intent.setClass(GroupNotFriendActivity.this, VerificationActivity.class);
                    //对方信息
                    intent.putExtra("detail_add_friend", mUserName);
                    intent.putExtra("detail_add_nick_name", mNickName);
                    intent.putExtra("detail_add_avatar_path", mAvatarPath);
                    //自己的昵称或者是用户名
                    intent.putExtra("detail_add_friend_my_nickname", mMyName);
                    intent.setFlags(1);
                    startActivity(intent);
                }
                break;
            case R.id.btn_send_message:
                intent.setClass(GroupNotFriendActivity.this, ChatActivity.class);
                //创建会话
                intent.putExtra(JGApplication.TARGET_ID, mUserInfo.getUserName());
                intent.putExtra(JGApplication.TARGET_APP_KEY, mUserInfo.getAppKey());
                String notename = mUserInfo.getNotename();
                if (TextUtils.isEmpty(notename)) {
                    notename = mUserInfo.getNickname();
                    if (TextUtils.isEmpty(notename)) {
                        notename = mUserInfo.getUserName();
                    }
                }
                intent.putExtra(JGApplication.CONV_TITLE, notename);
                Conversation conv = JMessageClient.getSingleConversation(mUserInfo.getUserName(), mUserInfo.getAppKey());
                //如果会话为空，使用EventBus通知会话列表添加新会话
                if (conv == null) {
                    conv = Conversation.createSingleConversation(mUserInfo.getUserName(), mUserInfo.getAppKey());
                    EventBus.getDefault().post(new Event.Builder()
                            .setType(EventType.createConversation)
                            .setConversation(conv)
                            .build());
                }
                startActivity(intent);
                break;
            case R.id.return_btn:
                finish();
                break;
            case R.id.iv_more:
                intent.setClass(GroupNotFriendActivity.this, NotFriendSettingActivity.class);
                intent.putExtra("notFriendUserName", mUserName);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
